Ext.define('Admin.view.organization.OrganizationViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.organizationViewController',

    onClickOrganizationRefreshButton: function(btn) {
        Ext.getCmp('organization-tree').getStore().reload();
    },

    onClickOrganizationPieButton: function(btn) {
        Ext.create('widget.organizationPieWindow');
    },

    onClickOrganizationAddButton: function(btn) {
        var win = Ext.create('widget.organizationFormWindow');
        win.down('form').getForm().findField('lead').setVisible(false);
    },

    onClickOrganizationEditButton: function(view, recIndex, cellIndex, item, e, record) {
        var win = Ext.create('widget.organizationFormWindow'),
            form = win.down('form').getForm();

        form.loadRecord(record);
        form.findField('lead').setRawValue(record.data.lead);

        if (record.data.parentId == 'root') {
            form.findField('parent').setVisible(false);
        }
    },

    onClickOrganizationDeleteButton: function(view, recIndex, cellIndex, item, e, record) {
        var tree = Ext.getCmp('organization-tree');

        Ext.Msg.show({
            title: '确定删除?',
            message: '你确定要删除选择的部门?',
            buttons: Ext.Msg.YESNO,
            icon: Ext.Msg.QUESTION,
            fn: function(btn) {
                if (btn === 'yes') {
                    Ext.Ajax.request({
                        url: 'organization/deleteOrganization',
                        method: 'post',
                        defaultHeaders: {
                            "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                        },
                        params: {
                            id: record.data.id
                        },
                        success: function(response, options) {
                            var json = Ext.util.JSON.decode(response.responseText);
                            if (json.code === 200) {
                                Ext.getCmp('organization-tree').getStore().reload();
                            } else {
                                Ext.Msg.alert('警告', json.message);
                            }
                        },
                        failure: function(response, options) {
                            var responseText = response.statusText;
                            if (response.status == 401 || response.status == 403) {
                                responseText = '缺少权限';

                            }
                            Ext.Msg.alert('错误', response.status + ' ' + responseText);
                        }
                    });
                }
            }
        });
    },

    onFormCancelButton: function(btn) {
        var win = btn.up('window');
        if (win) {
            win.close();
        }
    },

    onFormSubmitButton: function(btn) {
        var form = btn.up('form').getForm(),
            record = form.getRecord(),
            leadId = form.findField('lead').getValue(),
            lead = form.findField('lead').getRawValue(),
            parentId = form.findField('parent').getValue(),
            id;

        if (record == undefined) {
            id = null;
        } else {
            id = record.data.id;

            if (leadId == record.data.lead) {
                leadId = record.data.leadId;
            }

            if (parentId == record.data.parent) {
                parentId = record.data.parentId;
            }
        }

        Ext.Ajax.request({
            url: 'organization/saveOrUpdateOrganization',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            params: {
                id: id,
                name: form.findField('name').getValue(),
                leadId: leadId,
                lead: lead,
                parentId: parentId
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.code === 200) {
                    btn.up('window').close();
                    Ext.getCmp('organization-tree').getStore().reload();
                } else {
                    Ext.Msg.alert('警告', json.message);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;
                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';

                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    }

});